package boardgamer.yavalath.client.replaylobby;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import boardgamer.common.client.Display;
import boardgamer.common.client.layout.BitmapLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.MutableBitmapLayout;
import boardgamer.common.client.layout.MutableTextLayout;
import boardgamer.common.net.PlayerId;
import boardgamer.yavalath.client.RenderHelper;
import boardgamer.yavalath.client.pro.ProImageHandler;
import boardgamer.yavalath.client.resources.ImageHandler;
import boardgamer.yavalath.common.YavalathPlayerState;

import static boardgamer.yavalath.client.lobby.PlayerNamePicker.MAX_CHAR_COUNT;
import static boardgamer.yavalath.client.lobby.PlayerNamePicker.TEXT_LEFT_HEIGHT_RATIO;
import static boardgamer.yavalath.client.lobby.PlayerNamePicker.TEXT_MAX_HEIGHT_RATIO;

/**
 * @author garysoed
 */
public class ReplayPlayerDisplay implements Display {

  private final MutableBitmapLayout playerColorDisplay;
  private final MutableBitmapLayout playerModeDisplay;
  private final ReplayPlayerNameDisplay playerNameDisplay;
  private final LayoutGroup group;
  private boolean isVisible;
  
  public ReplayPlayerDisplay() {
    this.playerColorDisplay = new MutableBitmapLayout(
        ProImageHandler.colorPickerDisabled,
        new Paint(Paint.ANTI_ALIAS_FLAG));
    
    Paint modePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    modePaint.setColorFilter(new LightingColorFilter(Color.RED, 0));
    this.playerModeDisplay = new MutableBitmapLayout(
        ImageHandler.humanButton,
        modePaint);
    this.playerNameDisplay = new ReplayPlayerNameDisplay();
    this.group = new LayoutGroup()
        .add(playerColorDisplay).atCurrentRect()
        .add(playerModeDisplay).relativeToEdge(playerColorDisplay, LayoutGroup.Direction.RIGHT, 0)
        .add(playerNameDisplay).relativeToEdge(playerModeDisplay, LayoutGroup.Direction.RIGHT, 0);
    this.isVisible = false;
  }
  
  public void hide() {
    this.isVisible = false;
  }

  public void display(PlayerId player, YavalathPlayerState playerState) {
    this.isVisible = playerState.getMode() != YavalathPlayerState.PlayerMode.CLOSED;

    int color = RenderHelper.instance().getColor(playerState.getColor());
    Paint colorPaint = playerColorDisplay.getPaint();
    colorPaint.setColorFilter(new LightingColorFilter(color, 0));
    
    switch (playerState.getMode()) {
      case AI:
        playerModeDisplay.setBitmap(ProImageHandler.aiButtonDisabled);
        break;
      case HUMAN:
        playerModeDisplay.setBitmap(ProImageHandler.humanButtonDisabled);
        break;
      case CLOSED:
        break;
      default:
        throw new UnsupportedOperationException(
            "Unhandled player state: " + playerState.getMode());
    }
    playerNameDisplay.setPlayerName(player.getName());
  }

  @Override
  public void draw(Canvas canvas) {
    if (isVisible) {
      playerColorDisplay.draw(canvas);
      playerModeDisplay.draw(canvas);
      playerNameDisplay.draw(canvas);
    }
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }

  private static class ReplayPlayerNameDisplay implements Display{

    private final BitmapLayout background;
    private final MutableTextLayout playerNameText;
    private final LayoutGroup group;

    public ReplayPlayerNameDisplay() {
      Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      backgroundPaint.setColorFilter(new LightingColorFilter(Color.RED, 0));
      this.background
          = new BitmapLayout(ProImageHandler.playerNameBackgroundDisabled, backgroundPaint);

      Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      textPaint.setColor(Color.BLACK);
      textPaint.setTypeface(ImageHandler.headingFont);
      textPaint.setTextSize(RenderHelper.instance().getMaxFittingHeightTextSize(
          this.background.getBoundingRect().height() * TEXT_MAX_HEIGHT_RATIO,
          ImageHandler.headingFont));
      this.playerNameText = new MutableTextLayout(
          textPaint,
          background.getBoundingRect().width()
              - background.getBoundingRect().height() * TEXT_LEFT_HEIGHT_RATIO,
          MAX_CHAR_COUNT,
          LayoutGroup.HorizontalAlign.LEFT,
          ImageHandler.boundSizeRatio.get(ImageHandler.headingFont));

      this.group = new LayoutGroup()
          .add(background).atCurrentRect()
          .add(playerNameText).atPosition(
              background.getBoundingRect().height() * TEXT_LEFT_HEIGHT_RATIO,
              0)
          .verticalAlign(LayoutGroup.VerticalAlign.CENTER);
    }
    
    public void setPlayerName(String name) {
      playerNameText.setText(name);
    }

    @Override
    public void draw(Canvas canvas) {
      background.draw(canvas);
      playerNameText.draw(canvas);
    }

    @Override
    public void setBoundingRect(RectF boundingRect) {
      group.setBoundingRect(boundingRect);
    }

    @Override
    public RectF getBoundingRect() {
      return group.getBoundingRect();
    }
  }
}
